package maincode;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
import java.util.Scanner;

public class userInputController {

	public static void inputController()
	{
		Scanner in = new Scanner(System.in);
		int mainInput;
		do{				
			System.out.println("Display Today's Rate - 1, Display according to the number of days before - 2,Predict - 3,Exit - 4");
			mainInput=in.nextInt();
			//DISPLAY
			
			
			if(mainInput==1){
					//Display history records
					displayRecords();
			}
			else{
                
				if(mainInput==2){
					boolean validInput;
					String cur=currencyChoose();
					
					//36 duihuan
					int days;
					do{
						System.out.println("Please input the number of days ago: ");
						days = Integer.parseInt(in.next())/1;
						
					}while (days<=0 & days>100);
					//System.out.println(cur);
					displayPastRecords(cur,days);
				}
				//PREDICT
				else if(mainInput==3){
                    String cur=currencyChoose();
					displayExcRate(cur);
				}
			}
			if(mainInput==4){
				return;
			}
			inputController();
			
		} while(mainInput!=1&&mainInput!=2&&mainInput!=3);
	}
	
	public static String currencyChoose(){
        String cur;
        boolean validInput;
        Scanner in = new Scanner(System.in);
		do{
            System.out.println("Please choose one currency (USD CNY JPY EUR AUD GBP): ");
            cur = in.next();
            validInput=checkCurInput(cur);
            
        }while (validInput==false);
		
		return cur;
    }
	//Check user's input for currency
	public static boolean checkCurInput(String cur){

		String currency[] = new String[]{"USD","CNY","JPY","EUR","AUD","GBP"};

		for(int i=0; i<currency.length; i++){
			if(cur.equals(currency[i]))
				return true;				
		}
		
		return false;
	}
	
	public static int getCurIndex(String n){
		if(n.equalsIgnoreCase("USD"))
			return 0;
		else if(n.equalsIgnoreCase("CNY"))
			return 1;
		else if(n.equalsIgnoreCase("JPY"))
			return 2;
		else if(n.equalsIgnoreCase("EUR"))
			return 3;
		else if(n.equalsIgnoreCase("AUD"))
			return 4;
		else if(n.equalsIgnoreCase("GBP"))
			return 5;
		return -1;
		
	}
	
	//Display exchange rate on today
	public static void displayRecords(){
		System.out.println("Display Today's Rate");
		int size=currencyStream.curSet[0].getValues().length;
		for(int i=0;i<6;i++){
			//System.out.println(currencyStream.curSet[i].getValues()[size-2]);
			DecimalFormat df = new DecimalFormat( "0.00000 "); 
			System.out.println(currencyStream.curSet[i].getName()+" to HKD: "+df.format(currencyStream.curSet[i].getRateValues()[size-1]));
			for(int j=0;j<6;j++){
				double value=currencyStream.curSet[i].getRateValues()[size-1]/currencyStream.curSet[j].getRateValues()[size-1];
				System.out.println(currencyStream.curSet[i].getName()+" to "+currencyStream.curSet[j].getName()+": "+
						df.format(value));
			}
		}
	}
	
	private static Object getValues() {
		// TODO Auto-generated method stub
		return null;
	}

		//Display exchange rate in specific day past 10 days
		public static void displayPastRecords(String cur1,int days){
			System.out.println("Display according to the number of days before");
			int index=getCurIndex(cur1);
			DecimalFormat df = new DecimalFormat( "0.00000 ");
			//System.out.println("*"+index);
			int size=currencyStream.curSet[index].getValues().length;
			for(int i=0;i<9;i++){
				System.out.print(df.format(currencyStream.curSet[index].getRateValues()[size-10+i-days])+", ");
			}
			System.out.print(df.format(currencyStream.curSet[index].getRateValues()[size-1-days])+".");
			System.out.println();
	}
		
	//Display predicted exchange rate result(base: HKD)
	public static void displayExcRate(String cur1){
		System.out.println("PREDICT");
		int index=getCurIndex(cur1);
		//System.out.println("*"+index);
		//System.out.println("Pridicted Nomalize Pre: "normalizePredRate.getNormalizedRate(7,index));
		double normal= normalizePredRate.getNormalizedRate(7,index);
		if(normal > 0){
			System.out.println("The Exchange Rate will increase.");
		}
		else if(normal < 0){
			System.out.println("The Exchange Rate will decrease.");
		}
		else{
			System.out.println("The Exchange Rate will remain.");
		}
	}
}
